package com.zg.datapush.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zg.common.core.R;
import com.zg.common.core.dao.datapush.entity.DpPushRecord;
import com.zg.datapush.vo.DpPushLogVo;
import com.zg.datapush.vo.DpPushRecordVo;
import com.zg.datapush.req.DpPushRecordIdReq;
import com.zg.datapush.req.DpPushRecordPageReq;
import com.zg.datapush.service.DpPushLogService;
import com.zg.datapush.service.DpPushRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 推送记录Controller（增强版）
 * @author zg
 */
@RestController
@RequestMapping("/datapush/record")
@RequiredArgsConstructor
@Api(tags = "推送记录管理")
public class DpPushRecordController {

    private final DpPushRecordService pushRecordService;
    private final DpPushLogService pushLogService;

    @ApiOperation("分页查询推送记录（支持多条件查询）")
    @PostMapping("/page")
    public R<IPage<DpPushRecordVo>> page(@RequestBody @Valid DpPushRecordPageReq req) {
        Page<DpPushRecord> page = new Page<>(req.getPage(), req.getPageSize());
        IPage<DpPushRecordVo> result = pushRecordService.page(page, req);
        return R.success(result);
    }

    @ApiOperation("查询推送记录详情（包含日志详情）")
    @PostMapping("/get")
    public R<DpPushRecordVo> getById(@RequestBody @Valid DpPushRecordIdReq req) {
        DpPushRecordVo result = pushRecordService.getById(req.getRecordId());
        return R.success(result);
    }

    @ApiOperation("查询推送日志详情")
    @PostMapping("/logs")
    public R<List<DpPushLogVo>> getLogs(@RequestBody @Valid DpPushRecordIdReq req) {
        List<DpPushLogVo> result = pushLogService.listByRecordId(req.getRecordId());
        return R.success(result);
    }

    @ApiOperation("手动重推")
    @PostMapping("/rePush")
    public R<String> rePush(@RequestBody @Valid DpPushRecordIdReq req) {
        return pushRecordService.rePush(req.getRecordId());
    }
}
